package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsGoodsMapper;
import com.qf.mapper.DtsStorageMapper;
import com.qf.mapper.DtsTopicMapper;
import com.qf.pojo.DtsGoods;
import com.qf.pojo.DtsStorage;
import com.qf.pojo.DtsTopic;
import com.qf.service.DtsTopicService;
import com.qf.util.AliyunOSSUtil;
import com.qf.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * projectName:workspace03
 * author:HardyWang
 * time:2021.11.30 19:41
 * description:
 */

@Service
public class DtsTopicServiceImpl implements DtsTopicService {

    @Autowired
    DtsTopicMapper topicMapper;
    @Autowired
    DtsGoodsMapper goodsMapper;
    @Autowired
    DtsStorageMapper storageMapper;

    @Value("${aliyun.oss.endpoint}")
    String endpoint;
    @Value("${aliyun.oss.accessKeyId}")
    String accessKeyId;
    @Value("${aliyun.oss.accessKeySecret}")
    String accessKeySecret;
    @Value("${aliyun.oss.bucketName}")
    String bucketName;

    @Override
    public Object create(DtsTopic topic) {

        if(topic == null){
            return ResponseUtil.badArgument();
        }

        //验证专题名
        LambdaQueryWrapper<DtsTopic> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(DtsTopic::getTitle,topic.getTitle());
        if (topicMapper.selectOne(wrapper)!=null){
            return ResponseUtil.fail(-1,"专题名已存在");
        }
        //验证商品是否存在
        String[] goods = topic.getGoods();
        if (StringUtils.isEmpty(goods)||goods.length==0){
            return ResponseUtil.fail(-1,"商品不能为空");
        }
        QueryWrapper<DtsGoods> wrapper1 = new QueryWrapper<>();
        wrapper1.in("id",goods);
        List<DtsGoods> dtsGoods = goodsMapper.selectList(wrapper1);
        if(dtsGoods.size() != goods.length){
            return ResponseUtil.fail(-1,"商品不存在");
        }

        topic.setAddTime(new Date());
        topic.setUpdateTime(new Date());
        topic.setDeleted(false);
        topicMapper.insert(topic);
        return ResponseUtil.ok(topic);
    }

    @Override
    public Object delete(DtsTopic topic) {

        if(topic == null){
            return ResponseUtil.badArgument();
        }
        //删除oss对象
        if (!StringUtils.isEmpty(topic.getPicUrl())) {
            deleteOss(topic);
        }

        if(topicMapper.deleteById(topic.getId())==0){
            return ResponseUtil.fail(507,"删除失败");
        }

        return ResponseUtil.ok();
    }

    @Override
    public Object update(DtsTopic topic) {



        if(topic == null){
            return ResponseUtil.badArgument();
        }

        DtsTopic dbTopic = topicMapper.selectById(topic);
        if(!dbTopic.getTitle().equals(topic.getTitle())){
            //验证专题名
            LambdaQueryWrapper<DtsTopic> wrapper=new LambdaQueryWrapper<>();
            wrapper.eq(DtsTopic::getTitle,topic.getTitle());
            if (topicMapper.selectOne(wrapper)!=null){
                return ResponseUtil.fail(508,"专题名已存在");
            }
        }

        //验证商品是否存在
        String[] goods = topic.getGoods();
        if (StringUtils.isEmpty(goods)){
            return ResponseUtil.fail(-1,"商品不能为空");
        }
        QueryWrapper<DtsGoods> wrapper1 = new QueryWrapper<>();
        wrapper1.in("id",goods);
        List<DtsGoods> dtsGoods = goodsMapper.selectList(wrapper1);
        if(dtsGoods.size() != goods.length){
            return ResponseUtil.fail(-1,"商品不存在");
        }
        //删除oss对象
        if (!topic.getPicUrl().equals(dbTopic.getPicUrl())) {
            deleteOss(dbTopic);
        }
        topic.setUpdateTime(new Date());
        topicMapper.updateById(topic);
        return ResponseUtil.ok(topic);
    }
    private void deleteOss(DtsTopic dbTopic) {



        if (!StringUtils.isEmpty(dbTopic.getPicUrl())){
            LambdaQueryWrapper<DtsStorage> wrapper1=new LambdaQueryWrapper<>();
            wrapper1.eq(DtsStorage::getUrl,dbTopic.getPicUrl());
            DtsStorage storage = storageMapper.selectOne(wrapper1);
            if(storage != null){


                AliyunOSSUtil.deleteFile(endpoint,accessKeyId,accessKeySecret,bucketName,storage.getName());
                storageMapper.deleteById(storage.getId());
            }
        }

    }

    @Override
    public IPage<DtsTopic> queryPage(Integer page, Integer limit, String title, String subtitle, String sort, String order) {
        //配置分页参数
        IPage<DtsTopic> ipage = new Page(page, limit);

        //配置除分页以外的条件
        QueryWrapper<DtsTopic> wrapper = new QueryWrapper<>();

        if(!StringUtils.isEmpty(title)){
            wrapper.like("title", title);
        }
        if(!StringUtils.isEmpty(subtitle)){
            wrapper.like("subtitle", subtitle);
        }

        if(order.equalsIgnoreCase("asc")){
            wrapper.orderByAsc(sort);
        } else{
            wrapper.orderByDesc(sort);
        }

        ipage =topicMapper.selectPage(ipage , wrapper);

        return ipage;
    }


}
